//
//  NSString+encryption.m
//  KFD
//
//  Created by JIRENTIANXIANG on 6/5/15.
//  Copyright (c) 2015 KFD. All rights reserved.
//

#import "NSString+encryption.h"
#import <CommonCrypto/CommonCrypto.h>
#import "NSData+encryption.h"
#import "Base64Utils.h"

@implementation NSString (encryption)

- (NSString *)md5{
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), digest );
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [result appendFormat:@"%02x", digest[i]];
    
    return result;
}

-(NSString*)AES128EncryptedStringWithKey:(NSString *)key
{
    return [Base64Utils encrypt:[[self dataUsingEncoding:NSUTF8StringEncoding] AES128EncryptedDataWithKey:key]];
}

@end
